﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using TV_Organizer.Properties;

namespace TV_Organizer.FileScanner
{
    /// <summary>
    /// Interaction logic for FilesEditWindow.xaml
    /// </summary>
    public partial class FileEditionWindow : Window
    {
        public FileEditionWindow(IEnumerable<File> filesToEdit)
        {
            if (filesToEdit == null || filesToEdit.Count() == 0)
                throw new ArgumentException("FilesEditionWindow.Ctor: filesToEdit should be a non empty collection of Files.");

            this.FilesToEdit = new ObservableCollection<File>(filesToEdit);

            InitializeComponent();

            if (this.FilesToEdit.Count == 1)
                this.Title = string.Format("Edit: {0}", this.FilesToEdit.First().Filename);
            else
                this.Title = string.Format("Edit {0} files", this.FilesToEdit.Count);

            this.File = File.GetCommonFileForEdition(filesToEdit);
            this.DataContext = this.File;

            var connector = ConnectorsManager.Instance.GetTVShowsConnector(Settings.Default.SelectedTVShowConnector);
            if (connector != null)
                this.ShowComboBox.ItemsSource = connector.Shows;
        }

        public ObservableCollection<File> FilesToEdit { get; private set; }

        public File File { get; private set; }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // TODO
            //if (!string.IsNullOrEmpty(this.ShowComboBox.Text)
            //    && (this.ShowComboBox.SelectedItem == null || this.ShowComboBox.SelectedItem.ToString() != this.ShowComboBox.Text))
            //{
            //    var l_message = string.Format("You entered a TVShow manually. Do you want to add the following show: {0} to your database ?", this.ShowComboBox.Text);
            //    if (MessageBox.Show(l_message, "Add a TV show ?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            //    {
            //        var l_show = new UserTVShow(this.ShowComboBox.Text);
            //        TVShow.TVShows.Add(l_show);
            //        this.File.MatchingShows.Add(l_show);
            //        this.File.SelectedShow = l_show;
            //    }
            //}

            var team = this.TeamTextBox.Text;
            if (!string.IsNullOrEmpty(team) && team != File.Team && !Settings.Default.Teams.Contains(team))
            {
                var l_message = string.Format("You entered a Team manually. Do you want to add the following team: {0} to your database ?", team);
                if (MessageBox.Show(l_message, "Add a team ?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    Settings.Default.Teams.Add(team);
                    Settings.Default.Save();
                    this.File.Team = team;
                }
            }

            foreach (var file in this.FilesToEdit)
            {
                this.File.ApplyChangedPropertiesToFile(file);
            }

            this.DialogResult = true;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
